package gui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import classes.ConnectionDB;
import classes.ExamGeneralDetails;

import java.awt.SystemColor;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Exam.java
 *
 * Created on Aug 24, 2011, 1:27:38 PM
 */
/**
 * 
 * @author Fozy
 */
public class Exam extends javax.swing.JFrame {

	private static JFrame myFrame;
	private boolean allOk;
	private ArrayList<JFrame> frames = new ArrayList<JFrame>();
	private ArrayList<ExamGeneralDetails> data = new ArrayList<ExamGeneralDetails>();

	/** Creates new form Exam */
	public Exam(Properties langFile) {
		getContentPane().setBackground(Color.DARK_GRAY);
		initComponents(langFile);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		myFrame = this;
	}

	public void parseSubtopics(String subText, ArrayList<String> arr) {
		String[] result = subText.split(",");
		for (int i = 0; i < result.length; i++)
			arr.add(result[i]);
	}

	public void updateSubtopics(String subText, ArrayList<String> arr) {
		String[] result = subText.split(",");
		for (int i = 0; i < result.length; i++)
			arr.set(i,result[i]);
	}
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents(final Properties langFile) {

		jPanel1 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		createBtn = new javax.swing.JButton();
		cancelBtn = new javax.swing.JButton();
		examNameField = new javax.swing.JTextField();
		numOfQues = new javax.swing.JTextField();
		lowestRate = new javax.swing.JTextField();
		highestRate = new javax.swing.JTextField();
		numOfTopics = new javax.swing.JTextField();
		hrsDuration = new javax.swing.JTextField();
		minDuration = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel1.setForeground(SystemColor.inactiveCaption);

		setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
		this.setUndecorated(true);
		setBackground(new java.awt.Color(204, 0, 51));

		jPanel1.setBackground(Color.DARK_GRAY);
		jPanel1.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel2.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel2.setText(langFile.getProperty("usr.menu.file.addExam.examName"));

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel3.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel3.setText(langFile
				.getProperty("usr.menu.file.addExam.numOfQuestions"));

		jLabel4.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel4.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel4.setText(langFile
				.getProperty("usr.menu.file.addExam.lowestQuesRate"));

		jLabel5.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel5.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel5.setText(langFile
				.getProperty("usr.menu.file.addExam.highestQuesRate"));

		jLabel6.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel6.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel6.setText(langFile
				.getProperty("usr.menu.file.addExam.hrsDuration"));

		jLabel7.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel7.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel7.setText(langFile
				.getProperty("usr.menu.file.addExam.minsDuration"));

		jLabel8.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
		jLabel8.setForeground(javax.swing.UIManager.getDefaults().getColor(
				"Button.highlight"));
		jLabel8.setText(langFile
				.getProperty("usr.menu.file.addExam.numberOfTopics"));

		createBtn.setText(langFile.getProperty("usr.exitBotton"));
		createBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame = new JFrame(
						"JOptionPane showMessageDialog example");
				final Connection connection = new ConnectionDB()
						.createConnection();
				java.sql.PreparedStatement statement;
				allOk = true;

				if ((examNameField.getText().isEmpty())
						|| (minDuration.getText().isEmpty())
						|| (hrsDuration.getText().isEmpty())
						|| (lowestRate.getText().isEmpty())
						|| (highestRate.getText().isEmpty())
						|| (numOfQues.getText().isEmpty())
						|| (numOfTopics.getText().isEmpty())) {

					JOptionPane.showMessageDialog(
							frame,
							langFile.getProperty("usr.menu.file.addExam.alert.emptyField"));
					allOk = false;
				} else {
					try {
						Integer.parseInt(minDuration.getText());
						Integer.parseInt(hrsDuration.getText());
						Integer.parseInt(lowestRate.getText());
						Integer.parseInt(highestRate.getText());
						Integer.parseInt(numOfQues.getText());
						Integer.parseInt(numOfTopics.getText());
					} catch (NumberFormatException e2) {
						JOptionPane.showMessageDialog(
								frame,
								langFile.getProperty("usr.menu.file.addExam.alert.noNumber"));
						allOk = false;
					}

					if (Integer.parseInt(lowestRate.getText()) > Integer
							.parseInt(highestRate.getText())) {
						JOptionPane.showMessageDialog(
								frame,
								langFile.getProperty("usr.menu.file.addExam.alert.questionRate"));
						allOk = false;
					}
				}

				ArrayList<String> subTopics = new ArrayList<String>();
				parseSubtopics(subtopics.getText(), subTopics);
				int sum = 0;
				for (int i = 0; i < subTopics.size(); i++) {
					sum += Integer.parseInt(subTopics.get(i));
				}

				if (sum > Integer.parseInt(numOfQues.getText())) {
					JOptionPane.showMessageDialog(
							frame,
							langFile.getProperty("usr.menu.file.addExam.alert.subTopicsErr"));
					allOk = false;
				}
				if (Integer.parseInt(numOfTopics.getText()) != subTopics.size()) {
					JOptionPane.showMessageDialog(
							frame,
							langFile.getProperty("usr.menu.file.addExam.alert.topicSumErr"));
					allOk = false;
				}

				if (allOk) {

					try {
						statement = connection
								.prepareStatement("SELECT ExamId FROM Exams WHERE ExamId  = ?");
						statement.setString(1, examNameField.getText());
						ResultSet result = statement.executeQuery();
						if (result.next()) {
							JOptionPane.showMessageDialog(
									frame,
									langFile.getProperty("usr.menu.file.addExam.alert.examExsist"));
						}
						
						else {
							if (!frames.isEmpty()) {
								frames.get(0).setVisible(true);
								data.get(0).updateValues(examNameField.getText(),
										Integer.parseInt(numOfTopics.getText()),
										Integer.parseInt(lowestRate.getText()),
										Integer.parseInt(highestRate.getText()),
										Integer.parseInt(numOfQues.getText()),
										Integer.parseInt(hrsDuration.getText()),
										Integer.parseInt(minDuration.getText()));
								updateSubtopics(subtopics.getText(),subTopics);
							} else {
								ExamGeneralDetails details = new ExamGeneralDetails(
										examNameField.getText(),
										Integer.parseInt(numOfTopics.getText()),
										Integer.parseInt(lowestRate.getText()),
										Integer.parseInt(highestRate.getText()),
										Integer.parseInt(numOfQues.getText()),
										Integer.parseInt(hrsDuration.getText()),
										Integer.parseInt(minDuration.getText()));
								data.add(details);
								new AddingDesiredQuestions(langFile, Integer
										.parseInt(numOfTopics.getText()),
										frames,data,subTopics).setVisible(true);
							}
						}
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

				}
			}
		});

		cancelBtn.setText(langFile
				.getProperty("usr.menu.update.details.cancel"));
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				myFrame.setVisible(false);
			}
		});

		JLabel lblSubtopicsPerTopic = new JLabel(
				langFile.getProperty("usr.menu.file.addExam.minNumOfQuestions"));
		lblSubtopicsPerTopic.setForeground(SystemColor.activeCaptionText);
		lblSubtopicsPerTopic.setFont(new Font("Tahoma", Font.BOLD, 15));

		subtopics = new JTextField();
		subtopics.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				subtopics.setText("");
			}
		});
		subtopics.setText(langFile
				.getProperty("usr.menu.file.addExam.insertMinQuesFormat"));
		subtopics.setColumns(10);

		javax.swing.GroupLayout gl_jPanel1 = new javax.swing.GroupLayout(
				jPanel1);
		gl_jPanel1
				.setHorizontalGroup(gl_jPanel1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_jPanel1
										.createSequentialGroup()
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																gl_jPanel1
																		.createSequentialGroup()
																		.addGroup(
																				gl_jPanel1
																						.createParallelGroup(
																								Alignment.LEADING,
																								false)
																						.addGroup(
																								gl_jPanel1
																										.createSequentialGroup()
																										.addGroup(
																												gl_jPanel1
																														.createParallelGroup(
																																Alignment.LEADING)
																														.addComponent(
																																jLabel2)
																														.addComponent(
																																jLabel4)
																														.addComponent(
																																jLabel3))
																										.addGap(21)
																										.addGroup(
																												gl_jPanel1
																														.createParallelGroup(
																																Alignment.LEADING,
																																false)
																														.addComponent(
																																examNameField,
																																GroupLayout.DEFAULT_SIZE,
																																112,
																																Short.MAX_VALUE)
																														.addComponent(
																																numOfQues)
																														.addComponent(
																																lowestRate)))
																						.addGroup(
																								gl_jPanel1
																										.createSequentialGroup()
																										.addComponent(
																												jLabel5)
																										.addGap(18)
																										.addComponent(
																												highestRate)))
																		.addGap(50)
																		.addGroup(
																				gl_jPanel1
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								jLabel8)
																						.addComponent(
																								jLabel7)
																						.addComponent(
																								lblSubtopicsPerTopic)
																						.addComponent(
																								jLabel6))
																		.addGap(132))
														.addGroup(
																gl_jPanel1
																		.createSequentialGroup()
																		.addComponent(
																				createBtn,
																				GroupLayout.PREFERRED_SIZE,
																				92,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(139)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_jPanel1
																		.createSequentialGroup()
																		.addGroup(
																				gl_jPanel1
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								hrsDuration,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								162,
																								Short.MAX_VALUE)
																						.addComponent(
																								minDuration,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								162,
																								Short.MAX_VALUE)
																						.addComponent(
																								numOfTopics,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								162,
																								Short.MAX_VALUE)
																						.addComponent(
																								subtopics,
																								Alignment.LEADING,
																								GroupLayout.PREFERRED_SIZE,
																								162,
																								GroupLayout.PREFERRED_SIZE))
																		.addGap(38))
														.addGroup(
																gl_jPanel1
																		.createSequentialGroup()
																		.addComponent(
																				cancelBtn,
																				GroupLayout.DEFAULT_SIZE,
																				190,
																				Short.MAX_VALUE)
																		.addContainerGap()))));
		gl_jPanel1
				.setVerticalGroup(gl_jPanel1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_jPanel1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																examNameField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																numOfTopics,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel8))
										.addGap(82)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																numOfQues,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblSubtopicsPerTopic)
														.addComponent(
																subtopics,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(82)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																lowestRate,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6)
														.addComponent(
																hrsDuration,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(100)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																highestRate,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel7)
														.addComponent(
																minDuration,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												gl_jPanel1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																createBtn,
																GroupLayout.PREFERRED_SIZE,
																34,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cancelBtn,
																GroupLayout.PREFERRED_SIZE,
																31,
																GroupLayout.PREFERRED_SIZE))));
		jPanel1.setLayout(gl_jPanel1);

		createBtn.getAccessibleContext().setAccessibleName("Createbtn");
		cancelBtn.getAccessibleContext().setAccessibleName("CancelBtn");
		examNameField.getAccessibleContext().setAccessibleName("examNameField");
		numOfQues.getAccessibleContext()
				.setAccessibleName("numOfQuestionField");
		lowestRate.getAccessibleContext()
				.setAccessibleName("LowestWeightField");
		highestRate.getAccessibleContext().setAccessibleName(
				"HighestWeightField");
		numOfTopics.getAccessibleContext().setAccessibleName("numOfTopics");
		hrsDuration.getAccessibleContext().setAccessibleName(
				"hourDurationField");
		minDuration.getAccessibleContext().setAccessibleName(
				"minuteDurationField");

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
		jLabel1.setText("Create new exam");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(509)
								.addComponent(jLabel1,
										GroupLayout.DEFAULT_SIZE, 581,
										Short.MAX_VALUE).addGap(2988))
				.addGroup(
						layout.createSequentialGroup()
								.addGap(291)
								.addComponent(jPanel1,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addGap(2924)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jLabel1)
								.addGap(32)
								.addComponent(jPanel1,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addGap(58)));
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new Exam(null).setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton createBtn;
	private javax.swing.JButton cancelBtn;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JTextField examNameField;
	private javax.swing.JTextField numOfQues;
	private javax.swing.JTextField lowestRate;
	private javax.swing.JTextField highestRate;
	private javax.swing.JTextField numOfTopics;
	private javax.swing.JTextField hrsDuration;
	private javax.swing.JTextField minDuration;
	private JTextField subtopics;
}
